package sk.tuke.android4all.keyboard;

import sk.tuke.android4all.keyboard.R;
import sk.tuke.android4all.services.Tts;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.media.AudioManager;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
import android.widget.Button;
import android.widget.TextView;

public class SimpleIME extends InputMethodService implements OnKeyboardActionListener{
 
	private KeyboardView kv;
	private Keyboard keyboard;
	private Tts mSpeechEngine = null;
	 
	private boolean caps = false;
	private boolean nums = false;
	
	
	@Override
	public void onKey(int primaryCode, int[] keyCodes) {       
		InputConnection ic = getCurrentInputConnection();
	    playClick(primaryCode);
	    switch(primaryCode){
	    case -7:
	    	ExtractedTextRequest request = new ExtractedTextRequest();
	    	
	    	String enteredText = ic.getExtractedText(request, 0).text.toString();//ic.getTextAfterCursor(0, 1).toString();

	    	mSpeechEngine.sayMessage(enteredText);
	    	break;
	    case -6:
	    	nums = !nums;
	        kv.invalidateAllKeys();
	        if(nums){
	        	keyboard = new Keyboard(this, R.xml.qwerty_nums);
	        }else {
	        	keyboard = new Keyboard(this, R.xml.qwerty);
	        }
	        kv.setKeyboard(keyboard);
	        caps = false;
	    	break;
	    case Keyboard.KEYCODE_DELETE :
	        ic.deleteSurroundingText(1, 0);
	        break;
	    case Keyboard.KEYCODE_SHIFT:
	        caps = !caps;
	        kv.invalidateAllKeys();
	        if(caps){
	        	keyboard = new Keyboard(this, R.xml.qwerty_shifted);
	        }else {
	        	keyboard = new Keyboard(this, R.xml.qwerty);
	        }
	        kv.setKeyboard(keyboard);
	        nums = false;
	        break;
	    case Keyboard.KEYCODE_DONE:
	    	Log.i("Tag", ic.getSelectedText(0).toString());
	        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
	        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
	        break;
	    default:
	        char code = (char)primaryCode;
	        if(Character.isLetter(code) && caps){
	            code = Character.toUpperCase(code);
	        }
	        ic.commitText(String.valueOf(code),1);                 
	    }  
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){
		Log.i("Keyboard", "KeyDown");
		return false;
	}
		
	@Override
	public View onCreateInputView() {
	    kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null );
	    keyboard = new Keyboard(this, R.xml.qwerty);
	    kv.setKeyboard(keyboard);
	    kv.setOnKeyboardActionListener(this); 
	    kv.setOnTouchListener(new OnTouchListener() {

	    	int lastKeyPlayed = -1;
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				int eventX = (int)arg1.getX();
				int eventY = (int)arg1.getY();
				Key aSelectedKey = null;
				for(Key k : keyboard.getKeys()){
					if(eventX> k.x && eventX< (k.x + k.width)){
						if(eventY > k.y && eventY< (k.y + k.height)){
							aSelectedKey = k;
							break;
						}
					}
				}
				if(aSelectedKey != null){
					int playedCode = aSelectedKey.codes[0];
					if(playedCode != lastKeyPlayed){
						lastKeyPlayed = playedCode;
						playClick(playedCode);
					}
				}
				return false;
			}});
	    return kv;
	}
	
	private void playClick(int keyCode){
		if(mSpeechEngine == null){
			mSpeechEngine = new Tts(this);
		}
	    switch(keyCode){
	    case -7:
	    	mSpeechEngine.sayMessage("Read text");
	    	break;
	    case 33:
	    	mSpeechEngine.sayMessage("Exclamation mark");
	    	break;
	    case 47:
	    	mSpeechEngine.sayMessage("Backslash");
	    	break;
	    case -6:
	    	mSpeechEngine.sayMessage("Numlock");
	    	break;
	    case 32:
	        mSpeechEngine.sayMessage("Space");
	        break;
	    case Keyboard.KEYCODE_DONE: 
	    	mSpeechEngine.sayMessage("Done");
	    	break;
	    case 10:
	    	mSpeechEngine.sayMessage("Return");
	        break;
	    case Keyboard.KEYCODE_DELETE:
	    	mSpeechEngine.sayMessage("Delete");
	        break;
	    case Keyboard.KEYCODE_SHIFT:
	    	mSpeechEngine.sayMessage("Caps lock");
	    	break;
	    default: 
	    	if(Character.isLetterOrDigit(keyCode)){
		    	if(!caps){
		    			mSpeechEngine.sayMessage(String.valueOf(Character.toChars(keyCode)));
			    		}else{
			    			if(Character.isLetter(keyCode)){
			    				mSpeechEngine.sayMessage("Capital " + String.valueOf(Character.toChars(keyCode)));
			    			}
			    		}
					} 
	    		else{
	    			mSpeechEngine.sayMessage(Character.getName(keyCode));
	    		}
	    	}
	}

	@Override
	public void onPress(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onRelease(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onText(CharSequence arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void swipeDown() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void swipeLeft() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void swipeRight() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void swipeUp() {
		// TODO Auto-generated method stub
		
	}
}